package com.rdgk.newtech.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.dao.KeywordDao;
import com.rdgk.newtech.service.KeywordService;
import com.rdgk.newtech.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: msun
 */
@Service
public class KeywordServiceImpl implements KeywordService{
    @Autowired
    private KeywordDao keywordDao;
    private int is_deleted = 0;
    private int keywordId = -1;

    /**
     * 显示关键词列表
     */
    @Override
    public JSONObject listKeyword(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        System.out.println("jsonObject:"+jsonObject);
        int count = keywordDao.countKeyword(jsonObject);
        System.out.println("count:"+count);
        List<JSONObject> list = keywordDao.listKeyword(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 搜索关键词列表项
     */
    @Override
    public JSONObject listKeywordBySearch(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        System.out.println("jsonObject:"+jsonObject);
        int count = keywordDao.countKeywordtBySearch(jsonObject);
        System.out.println("count:"+count);
        List<JSONObject> list = keywordDao.listKeywordBySearch(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 按名称查询关键词是否存在
     */
    @Override
    public JSONObject getKeywordCountByName(JSONObject jsonObject) {
        List<JSONObject> list= keywordDao.getKeywordCountByName(jsonObject);
        if (list.size() >= 1) {
            is_deleted = list.get(0).getIntValue("is_deleted");
            if (is_deleted == 0){
                return CommonUtil.successJson("exist");
            }else{
                keywordId = list.get(0).getIntValue("id");
            }
        } else {
            return CommonUtil.successJson("nonexist");
        }
        return CommonUtil.successJson("nonexist");
    }

    /**
     * 新增关键词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject addKeyword(JSONObject jsonObject) {
        List<JSONObject> list = keywordDao.getTopicCountByName(jsonObject);
        Date now = new Date();
        jsonObject.put("createAt", now);
        jsonObject.put("updateAt", now);
        if (list.size() == 0){
            jsonObject.put("keywordTopicId",keywordDao.addTopic(jsonObject));
        }else {
            jsonObject.put("keywordTopicId",list.get(0).getIntValue("id"));
        }
        if (is_deleted == 0){
            keywordDao.addKeyword(jsonObject);
        }else {
            jsonObject.put("keywordId",keywordId);
            keywordDao.updateKeyword(jsonObject);
        }
        return CommonUtil.successJson();
    }

    /**
     * 更新关键词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject updateKeyword(JSONObject jsonObject) {
        List<JSONObject> list = keywordDao.getTopicCountByName(jsonObject);
        Date now = new Date();
        jsonObject.put("createAt", now);
        jsonObject.put("updateAt", now);
        if (list.size() == 0){
            keywordDao.addTopic(jsonObject);
        }
        jsonObject.put("keywordTopicId",list.get(0).getIntValue("id"));
        keywordDao.updateKeyword(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 删除关键词
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deleteKeyword(JSONObject jsonObject) {
        Date now = new Date();
        jsonObject.put("deleteAt", now);
        keywordDao.deleteKeyword(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 获取关键词主题列表项
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject listTopic(JSONObject jsonObject) {
        List<JSONObject> list = keywordDao.listTopic(jsonObject);
        return CommonUtil.successPage(list);
    }

//    /**
//     * 增加关键词主题列表项
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public JSONObject addTopic(JSONObject jsonObject) {
//        keywordDao.addTopic(jsonObject);
//        return CommonUtil.successJson();
//    }
}
